package com.google.android.volley;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Iterator;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.LayeredSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;

import android.content.Context;
import android.os.Build;
import android.util.Log;

public class GoogleHttpClient implements HttpClient {

    private final HttpClient mClient;

    private final ThreadLocal<Boolean> mConnectionAllocated=new ThreadLocal<Boolean>();

    public GoogleHttpClient(Context context, String s, boolean flag) {
        String s1=
            new StringBuilder(s).append(" (").append(Build.DEVICE).append(" ").append(Build.ID).append(")").toString();
        if(flag)
            s1=new StringBuilder(s1).append("; gzip").toString();
        mClient=AndroidHttpClient.newInstance(s1, context);
        SchemeRegistry schemeregistry=getConnectionManager().getSchemeRegistry();
        Iterator<String> iterator=schemeregistry.getSchemeNames().iterator();
        while(iterator.hasNext()) {
            String s2=iterator.next();
            Scheme scheme=schemeregistry.unregister(s2);
            SocketFactory socketfactory=scheme.getSocketFactory();
            Object obj;
            if(socketfactory instanceof LayeredSocketFactory)
                obj=new WrappedLayeredSocketFactory((LayeredSocketFactory)socketfactory);
            else
                obj=new WrappedSocketFactory(socketfactory);
            schemeregistry.register(new Scheme(s2, ((SocketFactory)(obj)), scheme.getDefaultPort()));
        }
    }

    @Override
    public HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
        return execute(request, (HttpContext)null);
    }

    @Override
    public HttpResponse execute(HttpUriRequest httpurirequest, HttpContext httpcontext) throws IOException, ClientProtocolException {
        String s=httpurirequest.getURI().toString();
        // com.google.android.common.http.UrlRules.Rule rule = UrlRules.getRules(mResolver).matchRule(s);
        String s1=s;// rule.apply(s);
        if(s1 == null) {
            Log.w("GoogleHttpClient", (new StringBuilder()).append("Blocked by ").append(/* rule.mName */"").append(": ").append(s)
                .toString());
            throw new BlockedRequestException(/* rule */);
        }
        HttpResponse httpresponse=null;
        if(s1 == s) {
            httpresponse=executeWithoutRewriting(httpurirequest, httpcontext);
        }/*
          * else { URI uri; RequestWrapper requestwrapper; try { uri = new URI(s1); } catch(URISyntaxException urisyntaxexception) {
          * throw new RuntimeException((new StringBuilder()).append("Bad URL from rule: ").append(rule.mName).toString(),
          * urisyntaxexception); } requestwrapper = wrapRequest(httpurirequest); requestwrapper.setURI(uri); httpresponse =
          * executeWithoutRewriting(requestwrapper, httpcontext); }
          */
        return httpresponse;
    }

    @Override
    public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
        return mClient.execute(target, request);
    }

    @Override
    public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws IOException,
        ClientProtocolException {
        return mClient.execute(request, responseHandler);
    }

    @Override
    public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException,
        ClientProtocolException {
        return mClient.execute(target, request, context);
    }

    @Override
    public <T> T execute(HttpUriRequest arg0, ResponseHandler<? extends T> arg1, HttpContext arg2) throws IOException,
        ClientProtocolException {
        return mClient.execute(arg0, arg1, arg2);
    }

    @Override
    public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2) throws IOException,
        ClientProtocolException {
        return mClient.execute(arg0, arg1, arg2);
    }

    @Override
    public <T> T execute(HttpHost arg0, HttpRequest arg1, ResponseHandler<? extends T> arg2, HttpContext arg3) throws IOException,
        ClientProtocolException {
        return mClient.execute(arg0, arg1, arg2, arg3);
    }

    @Override
    public ClientConnectionManager getConnectionManager() {
        return mClient.getConnectionManager();
    }

    @Override
    public HttpParams getParams() {
        return mClient.getParams();
    }

    public HttpResponse executeWithoutRewriting(HttpUriRequest httpurirequest, HttpContext httpcontext) throws IOException {
        // long l=SystemClock.elapsedRealtime();
        HttpResponse httpresponse1;
        // HttpEntity httpentity=null;
        mConnectionAllocated.set(null);
        // int i1;
        // long l3;
        // long l4;
        // i1=Process.myUid();
        // l3=TrafficStats.getUidTxBytes(i1);
        // l4=TrafficStats.getUidRxBytes(i1);
        httpresponse1=mClient.execute(httpurirequest, httpcontext);

        // if(httpresponse1 != null) {
        // goto _L4; else goto _L3
        // httpentity=httpresponse1.getEntity();
        // }
        // if(httpentity != null) {
        // long l5=SystemClock.elapsedRealtime();
        // long l6=l5 - l;
        // NetworkStatsEntity networkstatsentity = new NetworkStatsEntity(httpentity, mAppName, i1, l3, l4, l6, l5);
        // httpresponse1.setEntity(networkstatsentity);
        // }

        // }
        // int j=httpresponse1.getStatusLine().getStatusCode();
        // long l2=SystemClock.elapsedRealtime() - l;
        // int k;
        // if(mConnectionAllocated.get() != null || j < 0) {
        // k=1;
        // } else {
        // k=0;
        // EventLog.writeEvent(0x318fa, Long.valueOf(l2), Integer.valueOf(j), mAppName, Integer.valueOf(k));
        // }
        return httpresponse1;

    }

    private class WrappedLayeredSocketFactory extends WrappedSocketFactory implements LayeredSocketFactory {

        public final Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
            return mDelegate.createSocket(socket, s, i, flag);
        }

        private LayeredSocketFactory mDelegate;

        private WrappedLayeredSocketFactory(LayeredSocketFactory layeredsocketfactory) {
            super(layeredsocketfactory);
            mDelegate=layeredsocketfactory;
        }

    }

    private class WrappedSocketFactory implements SocketFactory {

        public final Socket connectSocket(Socket socket, String s, int i, InetAddress inetaddress, int j, HttpParams httpparams)
            throws IOException {
            mConnectionAllocated.set(Boolean.TRUE);
            return mDelegate.connectSocket(socket, s, i, inetaddress, j, httpparams);
        }

        public final Socket createSocket() throws IOException {
            return mDelegate.createSocket();
        }

        public final boolean isSecure(Socket socket) {
            return mDelegate.isSecure(socket);
        }

        private SocketFactory mDelegate;

        private WrappedSocketFactory(SocketFactory socketfactory) {
            super();
            mDelegate=socketfactory;
        }

    }

    public static class BlockedRequestException extends IOException {

        BlockedRequestException(/* com.google.android.common.http.UrlRules.Rule rule */) {
            // super((new StringBuilder()).append("Blocked by rule: ").append(rule.mName).toString());
        }
    }

}
